package com.it.emall.controller;

import com.it.emall.domain.AdminUser;
import com.it.emall.domain.Administrators;
import com.it.emall.service.AdministratorsService;
import com.it.emall.utils.JwtHelper;
import com.it.emall.utils.Md5;
import com.it.emall.utils.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 管理员登入
 */
@RestController
@RequestMapping("/admin/administrators")
public class AdministratorsController {

    @Autowired
    private AdministratorsService administratorsService;

    @PostMapping("/login")
    public Object login(@RequestBody Administrators administrators){
        //调用service
        Map<String,Object> json= new HashMap<>();
        Administrators loginuser = administratorsService.login(administrators.getUsername());
        System.out.println(loginuser.getPassword());
        String s = Md5.md5(administrators.getPassword());
        System.out.println(s);
        if(loginuser != null && loginuser.getPassword().equals(s)){
            System.out.println("登入成功");
            Map<String,Object> data = new HashMap<>();
            data.put("token", JwtHelper.createToken(loginuser.getId()));
            Map<String,Object> adminInfo = new HashMap<>();
            adminInfo.put("nikeName",loginuser.getUsername());
            adminInfo.put("avatar",loginuser.getAvatar());
            data.put("adminInfo",adminInfo);
            return ResponseUtils.ok(data);
        }else {
            System.out.println("失败");
            return ResponseUtils.fail(ResponseUtils.INVALID_TOKEN,"用户名或者密码错误");
        }
    }
}
